package com.wl.reflection;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

/**
 * 通过反射获取泛型
 */
public class Test03 {
    public static void main(String[] args) throws NoSuchMethodException {
        Method method = Test03.class.getMethod("test01", Map.class, List.class);
        //获取泛型的参数类型
        Type[] parameterTypes = method.getGenericParameterTypes();
        for (Type parameterType : parameterTypes) {
            System.out.println("parameterType = " + parameterType);
            if (parameterType instanceof ParameterizedType){
                Type[] arguments = ((ParameterizedType) parameterType).getActualTypeArguments();
                for (Type argument : arguments) {
                    System.out.println("argument = " + argument);
                }
            }
        }

        method = Test03.class.getMethod("test02",null);
        Type genericReturnType = method.getGenericReturnType();
        if (genericReturnType instanceof ParameterizedType){
            Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();
            for (Type actualTypeArgument : actualTypeArguments) {
                System.out.println("actualTypeArgument = " + actualTypeArgument);
            }
        }
    }

    public void test01(Map<String,User> map, List<User> list){
        System.out.println("test01");
    }

    public Map<String,User> test02(){
        System.out.println("test02");
        return null;
    }
}
